using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using Catastrophe.Common;
using SqlSugar;
using SqlSugarEx.Config;

namespace SqlSugarEx.Utilities
{
	public static class DbUtility
	{
		public static List<T> ConvertToEntity<T>(DataTable dt, int dstCount = -1) where T : class, new()
		{
			return DataConverter.ConvertToEntity<T>(dt, dstCount, (PropertyInfo property) => (Attribute.GetCustomAttribute(property, typeof(SugarColumn)) as SugarColumn)?.ColumnName);
		}

		public static T ConvertToEntity<T>(DataRow row) where T : class, new()
		{
			return DataConverter.ConvertToEntity<T>(row, (MemberInfo property) => (Attribute.GetCustomAttribute(property, typeof(SugarColumn)) as SugarColumn)?.ColumnName);
		}

		public static string SQLDbFormat(string host, string dbName, string userName, string pwd, int timeout = 1000)
		{
			DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(54, 5);
			defaultInterpolatedStringHandler.AppendLiteral("Server =");
			defaultInterpolatedStringHandler.AppendFormatted(host);
			defaultInterpolatedStringHandler.AppendLiteral("; Database =");
			defaultInterpolatedStringHandler.AppendFormatted(dbName);
			defaultInterpolatedStringHandler.AppendLiteral("; uid =");
			defaultInterpolatedStringHandler.AppendFormatted(userName);
			defaultInterpolatedStringHandler.AppendLiteral("; pwd =");
			defaultInterpolatedStringHandler.AppendFormatted(pwd);
			defaultInterpolatedStringHandler.AppendLiteral("; Connect Timeout = ");
			defaultInterpolatedStringHandler.AppendFormatted(timeout);
			return defaultInterpolatedStringHandler.ToStringAndClear();
		}

		public static string SQLDbFormat(string host)
		{
			return "Data Source=" + host + ";Initial Catalog=master;Integrated Security=True";
		}

		public static string SQLDbFormat(string host, string dbName)
		{
			DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(54, 2);
			defaultInterpolatedStringHandler.AppendLiteral("Data Source=");
			defaultInterpolatedStringHandler.AppendFormatted(host);
			defaultInterpolatedStringHandler.AppendLiteral(";Initial Catalog=");
			defaultInterpolatedStringHandler.AppendFormatted(dbName);
			defaultInterpolatedStringHandler.AppendLiteral(";Integrated Security=True");
			return defaultInterpolatedStringHandler.ToStringAndClear();
		}

		public static string MySqlFormat(string host, string dbName, int port, string userName, string pwd)
		{
			DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(58, 5);
			defaultInterpolatedStringHandler.AppendLiteral("server=");
			defaultInterpolatedStringHandler.AppendFormatted(host);
			defaultInterpolatedStringHandler.AppendLiteral(";User Id=");
			defaultInterpolatedStringHandler.AppendFormatted(userName);
			defaultInterpolatedStringHandler.AppendLiteral(";password=");
			defaultInterpolatedStringHandler.AppendFormatted(pwd);
			defaultInterpolatedStringHandler.AppendLiteral(";Database=");
			defaultInterpolatedStringHandler.AppendFormatted(dbName);
			defaultInterpolatedStringHandler.AppendLiteral(";port=");
			defaultInterpolatedStringHandler.AppendFormatted(port);
			defaultInterpolatedStringHandler.AppendLiteral(";SslMode = None;");
			return defaultInterpolatedStringHandler.ToStringAndClear();
		}

		public static string SqliteDbFormat(string dbName, string pwd = "", string dbPath = "", string version = "3")
		{
			return DbUtility.SqliteDbFormatWithoutVersion(dbName, pwd, dbPath);
		}

		public static string SqliteDbFormatWithoutVersion(string dbName, string pwd = "", string dbPath = "")
		{
			if (string.IsNullOrEmpty(dbPath))
			{
				dbPath = AppDomain.CurrentDomain.BaseDirectory;
			}
			if (!string.IsNullOrEmpty(pwd))
			{
				pwd = "Password = " + pwd + ";";
			}
			return "Data Source = " + Path.Combine(dbPath, dbName) + ".db;" + pwd;
		}

		public static string SqliteDbFormatSimple(string dbFullName, string pwd = "", string version = "3")
		{
			if (!File.Exists(dbFullName))
			{
				throw new Exception("文件不存在" + dbFullName);
			}
			if (!string.IsNullOrEmpty(pwd))
			{
				pwd = "Password = " + pwd + ";";
			}
			return "Data Source = " + dbFullName + ";" + pwd;
		}

		public static SqlSugarClientEx GetSugar(DbAppConfig dbConfig, bool isAutoCloseCon = false)
		{
			SqlSugarClientEx sqlSugarClientEx = null;
			switch (dbConfig.DbType)
			{
			case SqlSugar.DbType.SqlServer:
				sqlSugarClientEx = new SqlSugarClientEx(new ConnectionConfig
				{
					ConnectionString = DbUtility.SQLDbFormat(dbConfig.DbServer, dbConfig.DbName, dbConfig.DbUid, dbConfig.DbPwd, dbConfig.DbTimeOut),
					DbType = SqlSugar.DbType.SqlServer,
					IsAutoCloseConnection = isAutoCloseCon
				});
				break;
			case SqlSugar.DbType.Sqlite:
				sqlSugarClientEx = new SqlSugarClientEx(new ConnectionConfig
				{
					ConnectionString = DbUtility.SqliteDbFormat(dbConfig.DbName, dbConfig.DbPwd, dbConfig.DbPath),
					DbType = SqlSugar.DbType.Sqlite,
					IsAutoCloseConnection = isAutoCloseCon
				});
				break;
			case SqlSugar.DbType.MySql:
				sqlSugarClientEx = new SqlSugarClientEx(new ConnectionConfig
				{
					ConnectionString = DbUtility.MySqlFormat(dbConfig.DbServer, dbConfig.DbName, dbConfig.Port, dbConfig.DbUid, dbConfig.DbPwd),
					DbType = SqlSugar.DbType.MySql,
					IsAutoCloseConnection = isAutoCloseCon
				});
				break;
			}
			sqlSugarClientEx.CurrentConnectionConfig.InitKeyType = InitKeyType.Attribute;
			return sqlSugarClientEx;
		}
	}
}
